﻿#version 450 core

out vec4 color;

struct DirectionalLight {
    vec3 direction;
    vec4 color;
};
uniform DirectionalLight light;

uniform sampler2D gAlbedoSpec;
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gVerticeLight;
uniform float uTime;

void main() {
    // 从G-Buffer读取数据
    vec3 FragPos = texelFetch(gPosition, ivec2(gl_FragCoord.xy), 0).rgb  * 256.0;;
    vec3 Normal = ((texelFetch(gNormal, ivec2(gl_FragCoord.xy), 0) - 0.5) * 2.0).rgb;
    vec4 Albedo = texelFetch(gAlbedoSpec, ivec2(gl_FragCoord.xy), 0);
    float Specular = texelFetch(gAlbedoSpec, ivec2(gl_FragCoord.xy), 0).a;
    vec4 VerticeLight = texelFetch(gVerticeLight, ivec2(gl_FragCoord.xy), 0);

    if (Albedo.a < 0.1)
        discard;    

    // 计算光照
    float env = clamp(sin(uTime * 2 * 3.14159) * 0.125 + 0.75, 0.0, 0.8);
    float diff = max(dot(Normal, normalize(-light.direction)), 0.0);
    float lightIntensity = clamp(env + diff * light.color.w, 0.0, 1.0);


    // color = vec4((Albedo.xyz * ( VerticeLight.xyz)), 1.0);
    color = vec4((Albedo.xyz * clamp(light.color.xyz * lightIntensity + VerticeLight.xyz, vec3(0.0), vec3(1.2, 1.2, 1.0))), 1.0);

}